<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPerm(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="permList" row-key="id" default-expand-all="">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="scoped">
            <el-button v-if="scoped.row.type === 1" type="text" @click="addPerm(2,scoped.row.id)">添加</el-button>
            <el-button type="text" @click="editPerm(scoped.row.id)">编辑</el-button>
            <el-button type="text" @click="deletePerm(scoped.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗 -->

    <el-dialog title="新增权限" :visible="isShowDialog" @close="btnCancel">
      <el-form ref="permFrom" label-width="80px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="激活状态">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getPermList, getPermDetail, updatePerm, addPerm, deletePerm } from '@/api/permission.js'
import { listToTreeData } from '@/utils/index'
export default {
  name: '',
  components: {},
  props: {
  },
  data() {
    return {
      permList: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      },
      rules: {
        name: [
          { required: true, message: '权限名称不要为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限名称不要为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限名称不要为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getPermList()
  },
  mounted() {
  },
  methods: {
    // 获取权限列表
    async getPermList() {
      this.permList = await getPermList()
      console.log(48, this.permList)
      this.permList = listToTreeData(this.permList, '0')
    },
    // 编辑权限
    async editPerm(id) {
      // 获取数据，回显数据到编辑框
      this.formData = await getPermDetail(id)
      this.isShowDialog = true
      console.log(88, id)
    },
    // 添加权限
    addPerm(type, pid) {
      console.log(type, pid)
      // 初始化数据
      this.formData.type = type
      this.formData.pid = pid
      // 弹窗出现
      this.isShowDialog = true
    },
    // 删除权限
    async deletePerm(id) {
      await this.$confirm('确认删除此项吗')
      await deletePerm(id)
      this.$message.success('删除成功')
      // 更新数据
      this.getPermList()
    },
    async  btnOk() {
      // 校验表单
      await this.$refs.permFrom.validate()
      // 发起编辑请求
      // 发起添加请求
      // 需要判断是否为编辑还是添加部分，通过formData中的id来判断
      console.log('编辑')
      if (this.formData.id) {
        await updatePerm(this.formData)
        // 提示用户
        this.$message.success('编辑成功')
      } else {
        await addPerm(this.formData)
        this.$message.success('添加成功')
      }

      // 关闭弹窗
      this.isShowDialog = false
      // 更新数据
      this.getPermList()
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: ''
      }
      // 清理表单校验报错
      this.$refs.permFrom.resetFields()
      this.isShowDialog = false
    }
  }
}
</script>
<style scoped lang='less'>
</style>
